<template>
  <div class="user">
    <page-search
      :searchFormConfig="searchFormConfig"
      @resetBtnClick="resetBtnClick"
      @searchBtnClick="searchBtnClick"
    ></page-search>
    <page-content
      :contentConfig="contentConfig"
      pageName="users"
      ref="pageContentRef"
      @newBtnClick="addClick"
      @editBtnClick="editClick"
    ></page-content>

    <page-modal
      ref="pageModalRef"
      pageName="users"
      :defaultInfo="defaultInfo"
      :modalConfig="modalConfigRef"
    ></page-modal>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, computed } from 'vue'
import { useStore } from '@/store'

import { searchFormConfig } from './config/search.config'
import { contentConfig } from './config/content.config'
import { modalConfig } from './config/modal.config'

import PageSearch from '@/components/page-search/index'
import PageContent from '@/components/page-content/index'
import PageModal from '@/components/page-modal/index'

import { usePageSearch } from '@/hook/use-page-search'
import { usePageModal } from '@/hook/use-page-modal'

export default defineComponent({
  name: 'user',
  components: {
    PageSearch,
    PageContent,
    PageModal
  },
  setup() {
    // 重置/搜索 按钮点击-更新表格列表
    const [ pageContentRef, resetBtnClick, searchBtnClick ] = usePageSearch()

    // 新增/编辑 按钮点击
    // 处理是否展示某些特殊项

    const newBtnClick = () => {
      const passwordItem = modalConfig.formItems.find(item => {
        return item.field == 'password'
      })
      passwordItem!.isHidden = false
    }

    const editBtnClick = (item: any) => {
      const passwordItem = modalConfig.formItems.find(item => {
        return item.field == 'password'
      })
      passwordItem!.isHidden = true
    }

    const [ pageModalRef, defaultInfo, addClick, editClick ] = usePageModal(newBtnClick, editBtnClick)

    // 动态添加部门和角色列表
    const store = useStore()
    const modalConfigRef = computed(() => {
      const departmentItem = modalConfig.formItems.find(item => {
        return item.field === 'departmentId'
      })
      departmentItem!.options = store.state.departmentList.map(item => {
        return { label: item.name, value: item.id }
      })
      const roleItem = modalConfig.formItems.find(item => {
        return item.field === 'roleId'
      })
      roleItem!.options = store.state.roleList.map(item => {
        return { label: item.name, value: item.id }
      })

      return modalConfig
    })
    

    return {
      searchFormConfig,
      contentConfig,
      modalConfigRef,
      defaultInfo,
      resetBtnClick,
      searchBtnClick,
      pageContentRef,
      pageModalRef,
      addClick,
      editClick
    }
  }
})
</script>

<style scoped></style>
			
